package com.new_wear.project.system.nav.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.new_wear.common.utils.StringUtils;
import com.new_wear.framework.web.domain.Ztree;
import com.new_wear.project.system.area.domain.CstArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.new_wear.project.system.nav.mapper.CstNavMapper;
import com.new_wear.project.system.nav.domain.CstNav;
import com.new_wear.project.system.nav.service.ICstNavService;
import com.new_wear.common.utils.text.Convert;

/**
 * 系统 - 导航Service业务层处理
 * 
 * @author new_wear
 * @date 2020-01-07
 */
@Service
public class CstNavServiceImpl implements ICstNavService 
{
    @Autowired
    private CstNavMapper cstNavMapper;

    /**
     * 查询系统 - 导航
     * 
     * @param id 系统 - 导航ID
     * @return 系统 - 导航
     */
    @Override
    public CstNav selectCstNavById(Integer id)
    {
        return cstNavMapper.selectCstNavById(id);
    }

    /**
     * 查询系统 - 导航列表
     * 
     * @param cstNav 系统 - 导航
     * @return 系统 - 导航
     */
    @Override
    public List<CstNav> selectCstNavList(CstNav cstNav)
    {
        return cstNavMapper.selectCstNavList(cstNav);
    }

    /**
     * 新增系统 - 导航
     * 
     * @param cstNav 系统 - 导航
     * @return 结果
     */
    @Override
    public int insertCstNav(CstNav cstNav)
    {
        return cstNavMapper.insertCstNav(cstNav);
    }

    /**
     * 修改系统 - 导航
     * 
     * @param cstNav 系统 - 导航
     * @return 结果
     */
    @Override
    public int updateCstNav(CstNav cstNav)
    {
        return cstNavMapper.updateCstNav(cstNav);
    }

    /**
     * 删除系统 - 导航对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCstNavByIds(String ids)
    {
        return cstNavMapper.deleteCstNavByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统 - 导航信息
     * 
     * @param id 系统 - 导航ID
     * @return 结果
     */
    @Override
    public int deleteCstNavById(Integer id)
    {
        return cstNavMapper.deleteCstNavById(id);
    }

    /*树结构*/
    @Override
    public List<Ztree> menuTreeData() {
        CstNav cstNav = new CstNav();
        List<CstNav> cstNavList = selectCstNavList(cstNav);
        List<Ztree> ztrees = initZtree(cstNavList);
        return ztrees;
    }

    /**
     * 对象转菜单树
     *
     * @param cstNavList 菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<CstNav> cstNavList)
    {
        return initZtree(cstNavList, null, false);
    }

    /**
     * 对象转菜单树
     *
     cstVolunteerGroupList     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<CstNav> cstNavList, List<String> roleMenuList, boolean permsFlag)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleMenuList);
        for (CstNav cstNav : cstNavList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(cstNav.getId().longValue());
            ztree.setpId(cstNav.getPid().longValue());
            ztree.setName(transMenuName(cstNav, permsFlag));
            ztree.setTitle(cstNav.getName());
            if (isCheck)
            {
                ztree.setChecked(roleMenuList.contains(cstNav.getId() + ""));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public String transMenuName(CstNav cstNav, boolean permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(cstNav.getName());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + "" + "</font>");
        }
        return sb.toString();
    }
}
